using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class InventoryInfoPanel : MonoBehaviour {
	
	public UILabel WeightLabel;
	public UILabel EncumberanceLabel;
	
	public UILabel PlatinumLabel;
	public UILabel GoldLabel;
	public UILabel SilverLabel;
	public UILabel CopperLabel;
	
	public GameObject PlatinumBox;
	public GameObject GoldBox;
	public GameObject SilverBox;
	public GameObject CopperBox;
	
	public EquipableItemListDisplay equipItemList;
	public PassiveItemListDisplay passiveItemList;

	// Use this for initialization
	void Start () {
		UIButtonMessage message = PlatinumBox.AddComponent<UIButtonMessage>();
		message.target = gameObject;
		message.functionName = "PlatinumBoxClicked";
		
		message = GoldBox.AddComponent<UIButtonMessage>();
		message.target = gameObject;
		message.functionName = "GoldBoxClicked";
		
		message = SilverBox.AddComponent<UIButtonMessage>();
		message.target = gameObject;
		message.functionName = "SilverBoxClicked";
		
		message = CopperBox.AddComponent<UIButtonMessage>();
		message.target = gameObject;
		message.functionName = "CopperBoxClicked";
		
		ReloadDisplays();
	}
	
	// Update is called once per frame
	void Update () {
		if(SheetManager.activeCharacterUpdated)
		{		
			SheetManager.activeCharacterUpdated = false;
		
			if(equipItemList.toggled)
			{
				equipItemList.ToggleItemDisplays();
				equipItemList.ToggleItemDisplays();
			}
			if(passiveItemList.toggled)
			{
				passiveItemList.ToggleItemDisplays();
				passiveItemList.ToggleItemDisplays();
			}
			
			ReloadDisplays();
		}
	}
	
	void ReloadDisplays()
	{
		PlatinumLabel.text = SheetManager.activeCharacter.extraInfo.Platinum.ToString();
		GoldLabel.text = SheetManager.activeCharacter.extraInfo.Gold.ToString();
		SilverLabel.text = SheetManager.activeCharacter.extraInfo.Silver.ToString();
		CopperLabel.text = SheetManager.activeCharacter.extraInfo.Copper.ToString();
		
		WeightLabel.text = SheetManager.activeCharacter.CarryingWeight.ToString();
		EncumberanceLabel.text = SheetManager.activeCharacter.Encumberance.ToString();
	}
	
	IEnumerator PlatinumBoxClicked()
	{
		List<SetterWindowDescription> setterWindow = new List<SetterWindowDescription>{new SetterWindowDescription("", SheetManager.activeCharacter.extraInfo.Platinum.ToString(), true, null, false)};
		yield return StartCoroutine(HelperFunctions.SetterWindow("Platinum Pieces:", setterWindow));
		
		int p;
		if(int.TryParse(setterWindow[0].outVariable, out p))
		{
			SheetManager.activeCharacter.extraInfo.Platinum = p;
		}
		
		SheetManager.SaveActiveCharacter();
	}
	
	IEnumerator GoldBoxClicked()
	{
		List<SetterWindowDescription> setterWindow = new List<SetterWindowDescription>{new SetterWindowDescription("", SheetManager.activeCharacter.extraInfo.Gold.ToString(), true, null, false)};
		yield return StartCoroutine(HelperFunctions.SetterWindow("Gold Pieces:", setterWindow));
		
		int p;
		if(int.TryParse(setterWindow[0].outVariable, out p))
		{
			SheetManager.activeCharacter.extraInfo.Gold = p;
		}
		
		SheetManager.SaveActiveCharacter();
	}
	
	IEnumerator SilverBoxClicked()
	{
		List<SetterWindowDescription> setterWindow = new List<SetterWindowDescription>{new SetterWindowDescription("", SheetManager.activeCharacter.extraInfo.Silver.ToString(), true, null, false)};
		yield return StartCoroutine(HelperFunctions.SetterWindow("Silver Pieces:", setterWindow));
		
		int p;
		if(int.TryParse(setterWindow[0].outVariable, out p))
		{
			SheetManager.activeCharacter.extraInfo.Silver = p;
		}
		
		SheetManager.SaveActiveCharacter();
	}
	
	IEnumerator CopperBoxClicked()
	{
		List<SetterWindowDescription> setterWindow = new List<SetterWindowDescription>{new SetterWindowDescription("", SheetManager.activeCharacter.extraInfo.Copper.ToString(), true, null, false)};
		yield return StartCoroutine(HelperFunctions.SetterWindow("Copper Pieces:", setterWindow));
		
		int p;
		if(int.TryParse(setterWindow[0].outVariable, out p))
		{
			SheetManager.activeCharacter.extraInfo.Copper = p;
		}
		
		SheetManager.SaveActiveCharacter();
	}

}
